import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter/foundation.dart';

/// 应用路由观察者
/// 用于监听路由变化并执行相应操作（如分析、日志记录等）
class AppRouteObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);
    // 记录路由推入事件
    _logRouteEvent(
      'PUSH',
      current: route.settings.name,
      previous: previousRoute?.settings.name,
    );
    // 这里可以添加分析事件追踪
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    // 记录路由弹出事件
    _logRouteEvent(
      'POP',
      current: previousRoute?.settings.name,
      previous: route.settings.name,
    );
  }

  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    // 记录路由替换事件
    _logRouteEvent(
      'REPLACE',
      current: newRoute?.settings.name,
      previous: oldRoute?.settings.name,
    );
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didRemove(route, previousRoute);
    // 记录路由移除事件
    _logRouteEvent(
      'REMOVE',
      current: previousRoute?.settings.name,
      previous: route.settings.name,
    );
  }

  /// 记录路由事件
  void _logRouteEvent(
    String event, {
    String? current,
    String? previous,
  }) {
    if (kDebugMode) {
      print(
        '路由事件 [$event] - 当前: $current, 前一个: $previous',
      );
    }
    
    // 可以在此处添加分析事件记录
    // AnalyticsService.to.trackScreenView(current);
  }
} 